package com.xinpan.exercise;

import java.util.HashSet;
import java.util.Set;

public class ValidSudoku {
    public boolean isValidSudoku(char[][] board) {
        // Start typing your Java solution below
        // DO NOT write main() function
        Set<Character> rv = new HashSet<Character>();
        Set<Character> cv = new HashSet<Character>();
        Set<Character> gv = new HashSet<Character>();
        for(int i = 0; i < 9; i++)
        {
            for(int j = 0; j < 9; j++)
            {
                
                if(cv.contains(board[i][j]))
                    return false;
                else if(board[i][j] != '.')
                    cv.add(board[i][j]);
                if(rv.contains(board[j][i]))
                    return false;
                else if(board[j][i] != '.')
                    rv.add(board[j][i]);
                int r = i/3*3 + j/3;
                int c = i%3*3 + j%3;
                if(gv.contains(board[r][c]))
                    return false;
                else if(board[r][c] != '.')
                    gv.add(board[r][c]);
            }
            rv.clear();
            cv.clear();
            gv.clear();
        }
        return true;
    }
}
